package com.chenjt.jvm.third.GC;

import com.chenjt.jvm.first.six_methodArea.OOMTest;
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.Opcodes;

/**
 * 注释：
 *
 * java.lang.OutOfMemoryError: Metaspace异常演示
 *
 * -Xms60m -Xmx60m -XX:MetaspaceSize=10m -XX:MaxMetaspaceSize=10m -XX:SurviviorRatio=8 -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -XX:+PrintHeapAtGC -Xloggc:d:/MetaspaceOOM.log
 *
 *
 * @author chenjt
 * @date 2025/7/14 11:37
 */
public class MetaspaceOOM extends ClassLoader{

    public static void main(String[] args) {
        int j = 0;
        try {
            MetaspaceOOM test = new MetaspaceOOM();
            for (int i = 0; i < 10000; i++) {
                // 创建ClassWriter对象，用于生成类的二进制字节码
                ClassWriter classWriter = new ClassWriter(0);
                // 指明版本号，修饰符，类名，包名，父类，接口
                classWriter.visit(Opcodes.V1_8,Opcodes.ACC_PUBLIC,"Class" + i,null,"java/lang/Object",null);
                // 返回byte[]
                byte[] code = classWriter.toByteArray();
                // 类的加载
                test.defineClass("Class" + i,code,0,code.length);// Class对象
                j++;
            }
        } finally {
            System.out.println(j);
        }
    }

}
